﻿using System;
using System.Collections.Generic;
using System.Text;
using OnlineCourse.Model.Entity;
using OnlineCourse.Model;
using OnlineCourse.Services;
using OnlineCourse.Services.IServices;
using System.Linq;
namespace OnlineCourse.Services
{
    public  class PaymentTypeService : IPaymentTypeService
    {
        private readonly ApplicationDbContext _db;
        public PaymentTypeService(ApplicationDbContext db)
        {
            this._db = db;
        }
        public List<PaymentType> GetList()
        {
            return _db.PaymentTypes.ToList();
        }

        public bool AddPaymentType(PaymentType pt)
        {
            bool mark = false;
            try
            {
                _db.PaymentTypes.Add(pt);
                if(_db.SaveChanges()>0)
                {
                    mark = true;
                }
            }
            catch (Exception ee)
            {

                throw ee;
            }
            return mark;
        }

        public bool DeletePaymentType(Guid id)
        {
            bool mark = false;
            var obj= _db.PaymentTypes.FirstOrDefault(a => a.Id == id);
            if (obj != null)
            {
                try
                {
                    _db.PaymentTypes.Remove(obj);
                    if(_db.SaveChanges()>0)
                    {
                        mark = true;
                    }
                }
                catch (Exception ee)
                {

                    throw ee;
                }
            }

            return mark;
        }

        public bool UpdatePaymentType(Guid id, PaymentType pt)
        {
            bool mark = false;
            var m = _db.PaymentTypes.Where(a => a.Id == id).FirstOrDefault();
            if(!string.IsNullOrEmpty (pt.Title))
            {
                m.Title = pt.Title;
            }
            if(pt.Price>0)
            {
                m.Price = pt.Price;
            }
            if(pt.AccountMoney>0)
            {
                m.AccountMoney = pt.AccountMoney;
            }
            try
            {
                if(_db.SaveChanges()>0)
                {
                    mark = true;
                }
            }
            catch (Exception ee)
            {

                throw ee;
            }
            
            return mark;
        }
    }
}
